//
//  WSDataReportDetailViewController.m
//  microvision
//
//  Created by zhenjingxiong on 2021/3/8.
//  Copyright © 2021 tencent. All rights reserved.
//

#import "WSDataReportDetailViewController.h"
#import "WSDataReportConfig.h"
#import "WSDataReportHelper.h"
#import "WSDataReportFullScreenCell.h"
#import "WSDataReportToastView.h"

@interface WSDataReportDetailViewController ()<UISearchBarDelegate, UITableViewDataSource>

@property (nonatomic, copy) NSString *formattedString;
@property (nonatomic, strong) UITextView *reportContentTextView;
@property (nonatomic, strong) UITableView *mainInformationlistView;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UIButton *obtainMainInformationButton;

@end

@implementation WSDataReportDetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setUpUI];
}

#pragma mark - setUpUI Method

- (void)setUpUI {
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"数据上报详情页";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(closeCurrentPage)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"复制"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(copyReportDetailContent)];
    
    [self.view addSubview:self.searchBar];
    self.searchBar.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[self searchBarConstraints]];
    
    self.mainInformationlistView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.mainInformationlistView];
    [self.view addConstraints:[self mainInformationlistViewBarConstraints]];
    
    [self.view addSubview:self.obtainMainInformationButton];
    self.obtainMainInformationButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[self obtainMainInformationButtonConstraints]];
    
    [self.view addSubview:self.reportContentTextView];
    self.reportContentTextView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[self reportContentTextViewConstraints]];

    [self.mainInformationlistView  registerClass:[WSDataReportFullScreenCell class]
                          forCellReuseIdentifier:NSStringFromClass([WSDataReportFullScreenCell class])];
    self.formattedString = [WSDataReportHelper jsonStringPrettyPrintedFormatForDictionary:self.reportDataDictionary];
    self.reportContentTextView.text = self.formattedString;
}



#pragma mark - UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WSDataReportFullScreenCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WSDataReportFullScreenCell class]) forIndexPath:indexPath];
    [cell setShowLabelData:self.reportDataDictionary];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

#pragma mark - UISearchBarDelegate

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self searchKey:searchBar.text];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if (searchBar.text.length == 0) {
        [self performSelector:@selector(hideKeyboardWithSearchBar:) withObject:searchBar afterDelay:0];
    }
}

#pragma mark - Private Method

- (void)closeCurrentPage {
    if ([self.delegate respondsToSelector:@selector(goBackFromDetailViewController)]) {
        [self.delegate goBackFromDetailViewController];
    }
}

- (void)copyReportDetailContent {
    NSLog(@"打印一下1");
    UIPasteboard.generalPasteboard.string = self.formattedString;
    [WSDataReportToastView showWithStatus:@"复制成功"];
}

- (void)copyMainInformation {
    NSLog(@"打印一下2");
    NSMutableString *mutablString = [NSMutableString new];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    WSDataReportFullScreenCell *cell = [self.mainInformationlistView cellForRowAtIndexPath:indexPath];
    for (UILabel *label in cell.listShowLableArray) {
        [mutablString appendString:label.text];
        [mutablString appendString:@"\n"];
    }
    UIPasteboard.generalPasteboard.string = mutablString;
    [WSDataReportToastView showWithStatus:@"复制关键信息成功"];
}

- (void)hideKeyboardWithSearchBar:(UISearchBar *)searchBar {
    [self.view endEditing:YES];
    self.reportContentTextView.text = self.formattedString;
}


- (void)searchKey:(NSString *)searchText {
    NSMutableArray *locationArr = [WSDataReportHelper calculateSubStringCount:self.formattedString str:searchText];
    NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:self.formattedString];
    for (int i=0; i<locationArr.count; i++) {
        NSNumber *location = locationArr[i];
        NSRange locationRange = NSMakeRange(location.integerValue, searchText.length);
        [str2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:locationRange];
        [str2 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:locationRange];
    }
    self.reportContentTextView.attributedText = str2;
    [self.view endEditing:YES];

}

#pragma mark - Accessor Method

- (UISearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
        _searchBar.placeholder = @"请输入搜索的关键词";
        _searchBar.delegate = self;
    }
    return _searchBar;
}

- (UITableView *)mainInformationlistView {
    if (!_mainInformationlistView) {
        _mainInformationlistView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _mainInformationlistView.dataSource = self;
        _mainInformationlistView.scrollEnabled = NO;
        _mainInformationlistView.separatorStyle = UITableViewCellSeparatorStyleNone;
        NSArray *array = [WSDataReportConfig instance].listViewShowKeyArray;
        _mainInformationlistView.rowHeight = 20 * array.count + 5;
    }
    return _mainInformationlistView;
}

- (UIButton *)obtainMainInformationButton {
    if (!_obtainMainInformationButton) {
        _obtainMainInformationButton = [[UIButton alloc] init];
        [_obtainMainInformationButton setTitle:@"复制上面关键信息" forState:UIControlStateNormal];
        _obtainMainInformationButton.backgroundColor = [UIColor blackColor];
        [_obtainMainInformationButton addTarget:self action:@selector(copyMainInformation) forControlEvents:UIControlEventTouchUpInside];
    }
    return _obtainMainInformationButton;
}


- (UITextView *)reportContentTextView {
    if (!_reportContentTextView) {
        _reportContentTextView = [UITextView new];
        _reportContentTextView.editable = NO;
    }
    return _reportContentTextView;
}

#pragma mark - Automatic layout Method

- (NSArray<NSLayoutConstraint *> *)searchBarConstraints {
    NSLayoutConstraint *searchBarLeading = [WSDataReportHelper equallyRelatedConstraintWithView:self.searchBar toItem:self.view attribute:NSLayoutAttributeLeading];
    NSLayoutConstraint *searchBarTrailing = [WSDataReportHelper equallyRelatedConstraintWithView:self.searchBar toItem:self.view attribute:NSLayoutAttributeTrailing];
    NSLayoutConstraint *searchBartTop = [WSDataReportHelper equallyRelatedConstraintWithView:self.searchBar toItem:self.view attribute:NSLayoutAttributeTop];
    NSLayoutConstraint *searchBarHeight = [NSLayoutConstraint constraintWithItem:self.searchBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:50];
    NSArray *constraints = @[searchBarLeading, searchBarTrailing, searchBartTop, searchBarHeight];
    return constraints;
}


- (NSArray<NSLayoutConstraint *> *)mainInformationlistViewBarConstraints {
    NSLayoutConstraint *mainInformationlistViewLayoutConstraintLeading = [WSDataReportHelper equallyRelatedConstraintWithView:self.mainInformationlistView toItem:self.view attribute:NSLayoutAttributeLeading];
    NSLayoutConstraint *mainInformationlistViewLayoutConstraintTrailing = [WSDataReportHelper equallyRelatedConstraintWithView:self.mainInformationlistView toItem:self.view attribute:NSLayoutAttributeTrailing];
    NSLayoutConstraint *mainInformationlistViewLayoutConstraintTop = [NSLayoutConstraint constraintWithItem:self.mainInformationlistView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.searchBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5];
    NSLayoutConstraint *mainInformationlistViewHeight = [NSLayoutConstraint constraintWithItem:self.mainInformationlistView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:self.mainInformationlistView.rowHeight];
    NSArray *constraints = @[mainInformationlistViewLayoutConstraintLeading,
                             mainInformationlistViewLayoutConstraintTrailing,
                             mainInformationlistViewLayoutConstraintTop,
                             mainInformationlistViewHeight];
    return constraints;
}



- (NSArray<NSLayoutConstraint *> *)obtainMainInformationButtonConstraints {
    NSLayoutConstraint *obtainMainInformationButtonTrailing = [WSDataReportHelper equallyRelatedConstraintWithView:self.obtainMainInformationButton toItem:self.view attribute:NSLayoutAttributeTrailing];
    NSLayoutConstraint *obtainMainInformationButtonTop = [NSLayoutConstraint constraintWithItem:self.obtainMainInformationButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.mainInformationlistView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5];
    NSLayoutConstraint *obtainMainInformationButtonHeight = [NSLayoutConstraint constraintWithItem:self.obtainMainInformationButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:50];;
    NSArray *constraints = @[obtainMainInformationButtonTrailing, obtainMainInformationButtonTop, obtainMainInformationButtonHeight];
    return constraints;
}

- (NSArray<NSLayoutConstraint *> *)reportContentTextViewConstraints {
    NSLayoutConstraint *reportContentTextViewLeading = [WSDataReportHelper equallyRelatedConstraintWithView:self.reportContentTextView toItem:self.view attribute:NSLayoutAttributeLeading];
    NSLayoutConstraint *reportContentTextViewTrailing = [WSDataReportHelper equallyRelatedConstraintWithView:self.reportContentTextView toItem:self.view attribute:NSLayoutAttributeTrailing];
    NSLayoutConstraint *reportContentTextViewBottom = [WSDataReportHelper equallyRelatedConstraintWithView:self.reportContentTextView toItem:self.view attribute:NSLayoutAttributeBottom];
    NSLayoutConstraint *reportContentTextViewTop = [NSLayoutConstraint constraintWithItem:self.reportContentTextView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.obtainMainInformationButton attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5];
    NSArray *constraints = @[reportContentTextViewLeading,
                             reportContentTextViewTrailing,
                             reportContentTextViewBottom,
                             reportContentTextViewTop];
    return constraints;
}


@end
